package nl.rug.ml.dobbelen.learning;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

import nl.rug.ml.dobbelen.game.DobbelenGame;

/**
 * @author Do Bich Ngoc (bichngocdo89@gmail.com)
 * 
 */
public class DobbelenAction {

	public static final DobbelenActionComparator DOBBELEN_ACTION_COMPARATOR = new DobbelenActionComparator();

	private static Random random;

	private int[] histogram;
	private double value;
	private double egibility;

	static {
		random = new Random(System.currentTimeMillis());
	}

	public DobbelenAction(int[] histogram) {
		this.histogram = histogram;
		value = random.nextDouble() * DobbelenGame.MIN_SCORE;
		egibility = 0;
	}

	public int[] getHistogram() {
		return histogram;
	}

	public double getValue() {
		return value;
	}

	public void setValue(double value) {
		this.value = value;
	}

	public double getEgibility() {
		return egibility;
	}

	public void setEgibility(double egibility) {
		this.egibility = egibility;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(histogram);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof DobbelenAction)) {
			return false;
		}
		DobbelenAction other = (DobbelenAction) obj;
		if (!Arrays.equals(histogram, other.histogram)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append('[');
		for (int i = 0; i < histogram.length; i++) {
			sb.append(histogram[i]).append(' ');
		}
		if (sb.length() > 1) {
			sb.deleteCharAt(sb.length() - 1);
		}
		sb.append(']');
		return sb.toString();
	}

	public static class DobbelenActionComparator implements Comparator<DobbelenAction> {

		@Override
		public int compare(DobbelenAction o1, DobbelenAction o2) {
			if (o1.value > o2.value) {
				return 1;
			}
			else if (o1.value < o2.value) {
				return -1;
			}
			else {
				return 0;
			}
		}

	}

}
